/**
 * Project Name:Android_Car_Example
 * File Name:LocationTask.java
 * Package Name:com.amap.api.car.example
 * Date:2015年4月3日上午9:27:45
 */

package com.yxhl.zoume.core.func.map;

import android.content.Context;
import android.text.TextUtils;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.LocationSource;
import com.yxhl.zoume.core.func.map.info.PoiInfo;
import com.yxhl.zoume.utils.LOG;

import java.util.List;


/**
 * ClassName:LocationTask <br/>
 * Function: 简单封装了定位请求，可以进行单次定位和多次定位，注意的是在app结束或定位结束时注意销毁定位 <br/>
 * Date: 2015年4月3日 上午9:27:45 <br/>
 *
 * @author yiyi.qi
 * @see
 * @since JDK 1.6
 */
public class LocationTask implements LocationSource, AMapLocationListener,
        OnLocationGetListener {
    // 定位相关
    private static LocationTask mLocationTask;
    private final String TAG = "LocationTask";
    private Context mContext;
    private RegeocodeTask mRegecodeTask;
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationClientOption;
    private LocationSource.OnLocationChangedListener mOnLocationChangedListener;

    private boolean isStartLocate = false;


    private LocationTask(Context context) {
        mContext = context;
        mRegecodeTask = new RegeocodeTask(context);
        mRegecodeTask.setOnLocationGetListener(this);
    }

    public static LocationTask getInstance(Context context) {
        if (mLocationTask == null) {
            mLocationTask = new LocationTask(context);
        }
        return mLocationTask;
    }

    /**
     * 保存定位改变监听
     */
    public void setOnLocationChangedListener(OnLocationChangedListener listener) {
        mOnLocationChangedListener = listener;
    }

    public void startLocate(boolean onlyOnce) {
        initLocation();
        mLocationClientOption.setOnceLocation(onlyOnce);
        mLocationClient.setLocationOption(mLocationClientOption);
        mLocationClient.startLocation();
    }

    /**
     * 开启单次定位
     */
    public void startSingleLocate() {
        startLocate(true);
    }

    /**
     * 开启持续定位
     */
    public void startTrackLocate() {
        startLocate(false);
    }

    /**
     * 销毁定位资源
     */
    public void onDestroy() {
        if (null != mLocationClient) {
            mLocationClient.onDestroy();
        }
        isStartLocate = false;
    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (mOnLocationChangedListener != null && amapLocation != null) {

            if (amapLocation.getErrorCode() == 0) {
                // 显示系统小蓝点
                mOnLocationChangedListener.onLocationChanged(amapLocation);

                // 位置信息
                PoiInfo entity = new PoiInfo();
                entity.latitue = amapLocation.getLatitude();
                entity.longitude = amapLocation.getLongitude();

                if (!TextUtils.isEmpty(amapLocation.getAddress())) {
                    entity.address = amapLocation.getAddress();
                    entity.city = amapLocation.getCity();
                    entity.cityCode = amapLocation.getCityCode();
                }

                // TODO: 16/3/21 RRD Xboy ?数据处理
                // by Xboy
                // DataCenter.instance().getLocationData().setCurLocationPoi(entity);

                if (null != mOnLocationChangedListener) {
                    mOnLocationChangedListener.onLocationChanged(amapLocation);
                }
            } else {
                // 定位失败
                // TODO
                LOG.v("xboy", "定位失败");
            }
        }
    }

    public void initLocation() {
        if (null == mLocationClient) {
            mLocationClient = new AMapLocationClient(mContext);
            mLocationClientOption = new AMapLocationClientOption();
            //设置定位监听
            mLocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mLocationClient.setLocationOption(mLocationClientOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除

            // 不主动开启定位，此处使用单次定位
            //startSingleLocate();
        }
    }

    /**
     * 激活定位，结合地图使用
     */
    @Override
    public void activate(OnLocationChangedListener listener) {
        mOnLocationChangedListener = listener;
        initLocation();
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        mOnLocationChangedListener = null;
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }
        mLocationClient = null;
    }

    @Override
    public void onLocationGet(PoiInfo entity) {

    }

    @Override
    public void onRegecodeGet(List<PoiInfo> pois) {

    }
}
